﻿using Furion;
using Gear.Web.Api.Factory;
using Gear.Web.Api.DynamicRouter;
using Gear.Web.Api.DynamicCoding;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gear.Web.Api.DynamicDataApi;

namespace Gear.Web.Api
{
    [AppStartup(900)]
    public class WebApiStartup : AppStartup
    {
        /// <summary>
        /// ExamKingCore ConfigureServices
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddDynamicWebApi();

            // 动态编译注册
            services.AddSingleton<ICompiler, Compiler>();
            services.AddSingleton<DynamicActionProvider>();
            services.AddSingleton<DynamicChangeTokenProvider>();
            services.AddSingleton<IActionDescriptorProvider>(provider => provider.GetRequiredService<DynamicActionProvider>());
            services.AddSingleton<IActionDescriptorChangeProvider>(provider => provider.GetRequiredService<DynamicChangeTokenProvider>());

            // 动态路由控制
            services.AddSingleton<BrandRouteValueTransformer>();

            services.AddRouting().AddControllersWithViews();


        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDynamicControllerRoute<BrandRouteValueTransformer>("api/dyc/{controller}/{action}");// api/动态标识/[控制器]/[方法名]/[版本号]
                endpoints.MapControllerRoute(
                    name: default,
                    pattern: "{controller}/{action}"
                );
            });
            
        }
    }
}
